package com.hns.can.safety.web;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.web.BaseController;
import com.hns.can.safety.entity.BaseBlackArchivesInfoEntity;
import com.hns.can.safety.service.BaseBlackArchivesInfoService;
import com.hns.can.safety.vo.BaseBlackArchivesInfoVo;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 黑点档案信息Controller
 *
 * @version Revision 2.0.0
 * @author:jeffxu
 * @email:
 */
@Controller
@RequestMapping("/baseBlackArchivesInfo")
public class BaseBlackArchivesInfoController extends BaseController {

    @Autowired
    private BaseBlackArchivesInfoService baseBlackArchivesInfoService;

    //文件表头
    private final String[] tableHeaderNames = {"黑点名称", "黑点性质", "危险度", "最高限速", "区域路段"};
    //表头对应字段
    private final String[] tableHeaderFields = {"blackName", "blackNature", "dangerDegree", "highestSpdOfMotVhi", "areaRoad"};

    /**
     * 获取黑点档案信息列表
     *
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping("/searchBaseBlackArchivesInfoPage")
    @ResponseBody
    public Object searchBaseBlackArchivesInfoPage(BaseBlackArchivesInfoVo vo) throws Exception {
        Pagination page = baseBlackArchivesInfoService.searchBaseBlackArchivesInfoPage(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }

    /**
     * 通过id获取黑点档案信息
     *
     * @param rowidstr
     * @return
     * @throws Exception
     */
    @RequestMapping("/getBaseBlackArchivesInfoById")
    @ResponseBody
    public Object getBaseBlackArchivesInfoById(@RequestParam("rcrdId") String rcrdId) throws Exception {
        BaseBlackArchivesInfoEntity entity = baseBlackArchivesInfoService.getBaseBlackArchivesInfoById(rcrdId);
        return this.getReturnResult(entity);
    }

    /**
     * 添加黑点档案信息
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @RequestMapping("/addBaseBlackArchivesInfo")
    @ResponseBody
    public Object addBaseBlackArchivesInfo(BaseBlackArchivesInfoEntity entity) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        entity.setCorpId(user.getCorpId());
        entity.setCrtUser(user.getId());
        entity.setUpdateUser(user.getId());
        entity.setCrtTime(new Date());
        entity.setUpdateTime(new Date());

        baseBlackArchivesInfoService.addOrUptBaseBlackArchivesInfo(entity);
        return this.getReturnResult("新增成功");
    }

    /**
     * 编辑黑点档案信息
     *
     * @param entity
     * @return
     * @throws BusinessException
     */
    @RequestMapping("/uptBaseBlackArchivesInfo")
    @ResponseBody
    public Object uptBaseBlackArchivesInfo(BaseBlackArchivesInfoEntity entity) throws BusinessException {
        SysUserEntity user = this.getCurrentUser().getUser();
        entity.setUpdateUser(user.getId());
        entity.setUpdateTime(new Date());

        baseBlackArchivesInfoService.addOrUptBaseBlackArchivesInfo(entity);
        return this.getReturnResult("保存成功");
    }

    /**
     * 通过id删除黑点档案信息
     *
     * @param rcrdId
     * @return
     */
    @RequestMapping("/delBaseBlackArchivesInfoById")
    @ResponseBody
    public Object delBaseBlackArchivesInfoById(@RequestParam("rcrdId") String rcrdId) {
        baseBlackArchivesInfoService.delBaseBlackArchivesInfoById(rcrdId);
        return this.getReturnResult("删除成功");
    }

    /**
     * 获取黑点信息列表
     *
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping("/getBaseBlackArchivesInfoList")
    @ResponseBody
    public Object getBaseBlackArchivesInfoList(BaseBlackArchivesInfoVo vo) throws Exception {
        List<BaseEntity> list = baseBlackArchivesInfoService.getBaseBlackArchivesInfoList(vo);
        return this.getReturnResult(list);
    }

    /**
     * 导出excel
     *
     * @param vo
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/exportBaseBlackArchivesInfoExcel")
    public void exportBaseBlackArchivesInfoExcel(
            BaseBlackArchivesInfoVo vo, HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<BaseEntity> list = baseBlackArchivesInfoService.getBaseBlackArchivesInfoList(vo);
        ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "excel", "baseBlackArchivesInfo", "黑点档案信息");
    }

    /**
     * 导出pdf
     *
     * @param vo
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/exportBaseBlackArchivesInfoPdf")
    public void exportBaseBlackArchivesInfoPdf(
            BaseBlackArchivesInfoVo vo, HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<BaseEntity> list = baseBlackArchivesInfoService.getBaseBlackArchivesInfoList(vo);
        ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "pdf", "baseBlackArchivesInfo", "黑点档案信息");
    }

    /**
     * 图片上传
     *
     * @param myfiles         图片文件
     * @param uploadPath上传的路径
     * @return
     * @throws Exception
     * @return: Object
     */
    @RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
    @ResponseBody
    public Object uploadImage(@RequestParam MultipartFile[] myfiles, String uploadPath, HttpServletRequest request, HttpServletResponse response) throws Exception {
        baseBlackArchivesInfoService.fileUpload(myfiles, uploadPath, request, response);
        return null;
    }

    /**
     * 图片预览
     *
     * @param uploadPath
     * @param request
     * @param response
     * @return
     * @throws Exception
     * @return: Object
     * @RequestMapping(value = "/readImage", produces = "text/plain;charset=UTF-8")
     */
    @RequestMapping("/readImage")
    @ResponseBody
    public Object readImage(String uploadPath, HttpServletRequest request, HttpServletResponse response) throws Exception {
        baseBlackArchivesInfoService.readImage(uploadPath, request, response);
        return this.getReturnResult();
    }

}
